package pacman;

import java.awt.Point;

public class Info {

	final static int PILL_VALUE = 100;
	final static int POWERUP_VALUE = 125;
	final static int GHOST_VALUE = -3000;
	final static int EMPTY_VALUE = -1;
	final static int EATGHOST_VALUE = 200;
	final static int FRUIT_VALUE = 400;
	final static int MAX_LIVES = 3;
	
	private int value;			// the value perceive by agent as the value of the loc (factor in pill, ghost, etc)
	private Point p;			// the location of the node
	private int currentLives;
	
	// info about a given node/loc
	int pillCount;		// number of pills
	//int ghostCount;		// number of ghosts
	int ghostFound;	//which ghost was found
	boolean hasPill = false;	// whether given loc/node has a pill
	boolean hasPowerup = false;
	boolean hasGhost = false;	// whether given loc/node has a ghost (what happen more than one ghost in that loc?)
	boolean poweredUp = false;
	boolean hasFruit = false;

	public Info(Point p, GameModel model, int level) {
		this.p = p;
		retrieveData(model);
		//calculateValue();
		calculateValue(level);
	}
	
	public Point getPoint(){
		return p;
	}
	
	public int getValue() {
		return value;
	}
	
	void retrieveData(GameModel model) {
		int modelPoint = model.m_gameState[p.x][p.y];
		/*if ((modelPoint & model.GS_FOOD) == 1) {
			hasPill = true;
			hasPowerup = false;
			hasGhost = false;
		} else if ((modelPoint & model.GS_POWERUP) == 1) {
			hasPill = false
		}*/
		//hasPill = (modelPoint & GameModel.GS_FOOD) == 1;
		//hasPowerup = (modelPoint & GameModel.GS_POWERUP) == 1;
		hasPill = (modelPoint & GameModel.GS_FOOD) != 0;
		hasPowerup = (modelPoint & GameModel.GS_POWERUP) != 0;
		//hasGhost = checkGhosts(model);
		checkGhosts(model);
		//checkFruit(model);
		hasFruit = model.m_fruit.m_bAvailable && (p.x == model.m_fruit.m_locX && p.y == model.m_fruit.m_locY);
		//poweredUp = model.m_nTicksPowerup > 0;
		currentLives = model.m_nLives;
		
	}
	
	//boolean checkGhosts(GameModel model) {
	void checkGhosts(GameModel model) {
		//boolean ghostPresent = false;
		//Ghost[] ghosts = model.m_ghosts;
		for (int i = 0; i < model.m_ghosts.length; i++) {
			if (p.x == model.m_ghosts[i].m_locX && p.y == model.m_ghosts[i].m_locY) {
				//return true;
				//ghostFound = i;
				hasGhost = true;
				
				if (model.m_ghosts[i].m_nTicks2Flee > 10 /*&& model.m_ghosts[i].m_nTicks2Flee < 1000*/) {
					poweredUp = true;
				}
				
				return;
			}
			
		}
		
		//return false;
	}
	
	void checkFruit(GameModel model) {
		//boolean ghostPresent = false;
		//Ghost[] ghosts = model.m_ghosts;
		//for (int i = 0; i < model.m_ghosts.length; i++) {
			if (p.x == model.m_fruit.m_locX && p.y == model.m_fruit.m_locY) {
				//return true;
				hasFruit = true;
			}
			/*if (model.m_ghosts[i].m_nTicks2Flee > 0) {
				poweredUp = true;
			}
		}*/
		
		//return false;
	}
	
	void calculateValue() {
		value = 0;
		//value = pillCount * PILL_VALUE + ghostCount * GHOST_VALUE;
		value += (hasPill) ? PILL_VALUE * (MAX_LIVES - currentLives) : 0;
		value += (hasFruit) ? FRUIT_VALUE * currentLives : 0;
		
		if (poweredUp) {
			value += (hasGhost) ? EATGHOST_VALUE : 0;
		} else {
			value += (hasPowerup) ? POWERUP_VALUE : 0;
			value += (hasGhost) ? GHOST_VALUE * (MAX_LIVES - currentLives) : 0;
		}
			
		/*if (value == 0) {
			value = -1;
		}
		*/
	}
	
	/**
	 * 
	 * @param level
	 */
	private void calculateValue(int level) {
		value = 0;
		value += (hasPill) ? PILL_VALUE * (Agent.TREE_HEIGHT + 1 - level) : 0;
		value += (hasFruit) ? FRUIT_VALUE * (Agent.TREE_HEIGHT + 1 - level) : 0;
		
		if (poweredUp) {
			value += (hasGhost) ? EATGHOST_VALUE * (Agent.TREE_HEIGHT + 1 - level) : 0;
		} else {
			value += (hasPowerup) ? POWERUP_VALUE * (Agent.TREE_HEIGHT + 1 - level): 0;
			value += (hasGhost) ? GHOST_VALUE * (Agent.TREE_HEIGHT + 1 - level) * (MAX_LIVES - currentLives) : 0;
		}			
	}

	
}
